/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.examples.nmea.app;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;

import de.thwildau.tm.moses.examples.nmea.data.Nmea;
 
/**
 * The Handler class that forwards NMEA messages to a {@link NmeaMessageListener}.
 *
 * @author Thomas Kistel
 * @version $Revision: 764 $
 */
/* package */ final class NmeaMessageHandler extends SimpleChannelHandler {

    private final NmeaMessageListener messageListener;

    /**
     * @param nmeaPipelineFactory
     */
    public NmeaMessageHandler(NmeaMessageListener messageListener) {
        this.messageListener = messageListener;
    }

    @Override
    public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent e) throws Exception {
        Nmea nmeaMessage = (Nmea)e.getMessage();
        messageListener.newNmeaData(nmeaMessage);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
        Throwable cause = e.getCause();
        System.err.println("An Error occurred while decoding. " + cause.getClass().getName() + ": " + cause.getMessage());
    }
}
